<x-app-layout>
    <!-- 打印完整数据结构 -->
    <!-- @dump($messages) -->

    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Message') }}
        </h2>
    </x-slot>

    <div class="container mx-auto p-4">
        <div class="flex justify-between items-center mb-6">
            <h1 class="text-2xl font-bold">留言板目录</h1>
            <a href="{{ route('messageboard.create') }}" class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700">
                发布留言
            </a>
        </div>

        <!-- 搜索功能 -->
        <form action="{{ route('messageboard.list') }}" method="GET" class="mb-6">
            <div class="relative">
                <input
                    type="text"
                    name="query"
                    placeholder="搜索文章..."
                    value="{{ $query ?? '' }}"
                    class="w-full px-4 py-2 rounded-lg border focus:outline-none focus:ring-2 focus:ring-blue-500"
                >
                <button type="submit" class="absolute right-2 top-1/2 transform -translate-y-1/2 text-gray-500 hover:text-blue-500">
                    <i class="fa fa-search"></i>
                </button>
            </div>
        </form>

        <!-- 搜索结果提示 -->
        @if(isset($query) && $query)
            <div class="mb-4 px-4 py-3 bg-blue-50 rounded-lg border border-blue-200 text-blue-700">
                <p class="font-medium">搜索 "{{ $query }}" 的结果：共 {{ $messages->total() }} 条留言</p>
            </div>
        @endif

        <!-- 列表显示 -->
        <div class="space-y-6 mb-6">
            @foreach($messages as $message)
                <a href="{{ route('messageboard.show', $message) }}" class="block bg-white p-6 rounded-lg shadow-md text-blue-600">
                    <div class="flex items-center mb-4">
                        <img src="https://picsum.photos/id/{{ $message->user_id }}/40/40" alt="用户头像" class="w-10 h-10 rounded-full mr-3">
                        <div>
                            <h3 class="font-semibold text-gray-800">{{ $message->user->name }}</h3>
                            <p class="text-sm text-gray-500">{{ $message->created_at->format('Y-m-d H:i') }}</p>
                        </div>
                    </div>
                    <!-- to-do:可优化添加搜索词高亮显示 -->
                    <h2 class="text-xl font-bold mb-2">{{ $message->title }}</h2>
                    <p class="text-gray-700">{{ $message->content }}</p>
                </a>
            @endforeach
        </div>

        <!-- 分页链接 -->
        <div class="mt-8">
            <!-- {{ $messages->links() }} -->

            <!-- 分页增加搜索条件 -->
            {{ $messages->appends(request()->query())->links() }}
        </div>
    </div>
    @if(session('error'))
        <div class="alert alert-danger">
            {{ session('error') }}
        </div>
    @endif
</x-app-layout>